/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.tools {
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.display.ui.scroller.IScrollable;

	/**
	 * @author Marek Brun
	 */
	public class DateScopeScroller extends MLBroadcastClass implements IScrollable {

		private var dateStart:Date;
		private var dateEnd:Date;
		private var visibleTime:Number;
		private var scroll:Number=0;
		
		public function DateScopeScroller(dateStart:Date, dateEnd:Date, visibleTime:Number) {
			setup(dateStart, dateEnd, visibleTime);
		}
		
		override public function getPrivateVariables():Array {
			return [
				['dateStart', dateStart],
				['dateEnd', dateEnd],
				['visibleTime', visibleTime]].concat(super.getPrivateVariables());
		}
		
		public function get event_ScrollChanged():String { return 'ScrollChanged'; }
		
		public function setup(dateStart:Date, dateEnd:Date, visibleTime:Number=NaN):void {
			this.dateStart=dateStart;
			this.dateEnd=dateEnd;
			this.visibleTime=isNaN(visibleTime) ? this.visibleTime : visibleTime;
			broadcastEvent(event_ScrollChanged);
		}
		
		public function setVisibleTime(visibleTime:Number):void {
			this.visibleTime=visibleTime;
			broadcastEvent(event_ScrollChanged);
		}
		
		public function getScroll():Number {
			return scroll;
		}
		
		public function getVisibleToTotal():Number {
			var visible:Number=visibleTime;
			var total:Number=dateEnd.getTime()-dateStart.getTime();
			return visible/total;
		}
		
		public function setScroll(scroll:Number):void {
			this.scroll=Math.max(0, Math.min(1, scroll));
			broadcastEvent(event_ScrollChanged);
		}
		
		public function setScrollByOneUnit(isUp:Boolean):void {
			var total:Number=dateEnd.getTime()-dateStart.getTime()-visibleTime;
			setScroll(isUp ? scroll+visibleTime/total/2 : scroll-visibleTime/total/2);
		}

		public function getStartDate():Date {
			var date:Date=new Date();
			var total:Number=dateEnd.getTime()-dateStart.getTime()-visibleTime;
			var timeStart:Number=dateStart.getTime()+total*scroll;
			date.setTime(timeStart);
			return date;
		}
		
		public function getEndDate():Date {
			var date:Date=new Date();
			var total:Number=dateEnd.getTime()-dateStart.getTime()-visibleTime;
			var timeStart:Number=dateStart.getTime()+total*scroll;
			date.setTime(timeStart+visibleTime);
			return date;
		}
		
	}
}
